package com.pingan.glue.gluegame.controller;

import com.alibaba.fastjson.JSONObject;
import com.pingan.glue.gluegame.common.Constant;
import com.pingan.glue.gluegame.common.ErrorMessage;
import com.pingan.glue.gluegame.controller.form.FilmResponse;
import com.pingan.glue.gluegame.controller.form.GameOrderForm;
import com.pingan.glue.gluegame.controller.form.GameOrderResponse;
import com.pingan.glue.gluegame.entity.AwardContentEntity;
import com.pingan.glue.gluegame.entity.FriendUserEntity;
import com.pingan.glue.gluegame.entity.GameOrderEntity;
import com.pingan.glue.gluegame.response.ResponseData;
import com.pingan.glue.gluegame.service.GameOrderService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("game")
public class GameOrderController {
    private final static Logger LOGGER = LoggerFactory.getLogger(GameOrderController.class);

    @Resource
    private GameOrderService gameOrderService;

    @PostMapping
    public @ResponseBody
    ResponseData<GameOrderResponse> game(@RequestBody GameOrderForm form){
        GameOrderResponse response = new GameOrderResponse();
        LOGGER.info("GameOrderController_game_finishTime:{}",form.getFinishTime());
        FriendUserEntity entity = (FriendUserEntity) SecurityUtils.getSubject().getPrincipal();
        response.setThisTimeSpent(form.getFinishTime());
        Long time = gameOrderService.saveOrUpdateGameOrder(entity,form.getFinishTime());
        if(time != Long.MAX_VALUE){
             response.setLastTimeSpent(time);
        }

        List<GameOrderEntity> gameOrder = gameOrderService.queryGameOrder(form.getCurrent(),form.getSize());
        if (gameOrder == null){
            return ResponseData.error(ErrorMessage.GAME_QUERY_ERROR);
        }
        Long finishTime = time;
        if(form.getFinishTime() != null&& form.getFinishTime()< time){
            finishTime = form.getFinishTime();
        }
        int order = gameOrderService.getOrder(finishTime);
        String award = gameOrderService.calculateAward(entity,form.getFinishTime());
        response.setAward(award);
        response.setGameOrders(gameOrder);
        response.setTopRanking(finishTime==Long.MAX_VALUE?-1:order+1);
        return ResponseData.success(response);
    }

    @PostMapping("record")
    public @ResponseBody
    ResponseData<List<AwardContentEntity>> gameRecord(){
        LOGGER.info("GameOrderController_gameRecord: start");
        FriendUserEntity entity = (FriendUserEntity) SecurityUtils.getSubject().getPrincipal();
        List<AwardContentEntity> list = gameOrderService.record(entity.getMobile());
        return ResponseData.success(list);
    }


    @PostMapping("film")
    public @ResponseBody
    ResponseData<List<FilmResponse>> film(){
        if (Constant.FILM_RESPONSE_CATCH.isEmpty()){
            List<FilmResponse> filmResponseList = gameOrderService.film();
            Constant.FILM_RESPONSE_CATCH.addAll(filmResponseList);
        }
        return ResponseData.success(Constant.FILM_RESPONSE_CATCH);
    }

}
